package com.dangf.app.launcher.activity

import android.content.Intent
import android.os.Handler
import com.dangf.app.launcher.R
import com.dangf.app.launcher.app.BaseActivity
import com.dangf.app.launcher.app.HaisApp
import com.dangf.app.launcher.app.V
import com.dangf.app.launcher.dao.MainDao
import com.dangf.app.launcher.dialog.SetServerInfoWindows
import com.dangf.app.launcher.model.AdvertModel
import com.dangf.app.launcher.model.MachineModel
import com.dangf.app.launcher.model.SoftModel
import com.dangf.app.launcher.model.StoreModel
import com.yanzhenjie.nohttp.rest.Response
import com.yanzhenjie.nohttp.tools.NetUtils
import pw.hais.utils_lib.http.OnHttpListener
import pw.hais.utils_lib.utils.L

/**
 * Created by 管理员 on 2017/9/12.
 */
class InitActivity : BaseActivity(R.layout.activity_init) {
    private val setServerInfoWindows by lazy { SetServerInfoWindows(this) }

    override fun onInitViewsAndData() {
        Handler().postDelayed({
            if (NetUtils.isWifiConnected()) initDangFDate()
            else onInitViewsAndData()
        }, 1000)
    }

    fun initDangFDate() {
        if (V.SERVER_ADDRESS == null || V.HTTP_SERVER_PORT == 0 || V.SOCKECT_SERVER_PORT == 0) setServerInfoWindows.showContent()
        else MainDao.getStoreInfo(object : OnHttpListener<StoreModel> {
            override fun onSuccess(what: Int, response: Response<StoreModel>?, data: StoreModel) {
                if (data.status == 100) {
                    V.mStore = data.result
                    getMachineInfo()
                } else L.showLong("服务器链接出错，请设置服务器IP")
            }

            override fun onFailed(what: Int, response: Response<StoreModel>?, e: Exception?) {
                super.onFailed(what, response, e)
                L.showLong("服务器链接出错，请设置服务器IP")
                setServerInfoWindows.showContent()
            }
        })
    }

    private fun getMachineInfo() {
        MainDao.getMachineInfo(HaisApp.MachineCode, object : OnHttpListener<MachineModel> {
            override fun onSuccess(what: Int, response: Response<MachineModel>?, data: MachineModel) {
                if (data.status == 100) {
                    V.mMachine = data.result
                    getAdvert()
                } else {
                    L.showShort("机器未注册，请填写资料进行注册！")
                    startActivity(Intent(context, MachineRegisterActivity::class.java))
                    finish()
                }
            }
        })
    }

    private fun getAdvert() {
        MainDao.getAdvertLast(HaisApp.MachineCode, object : OnHttpListener<AdvertModel> {
            override fun onSuccess(what: Int, response: Response<AdvertModel>?, data: AdvertModel) {
                V.mAdvert = data.result
            }

            override fun onFinish(what: Int) {
                getAppList()
            }
        })
    }

    private fun getAppList() {
        MainDao.getAppList(V.mMachine!!.code, object : OnHttpListener<SoftModel> {
            override fun onSuccess(what: Int, response: Response<SoftModel>?, data: SoftModel) {
                data.result?.forEach {
                    if (it.type.contains("用户")) V.mAppList.add(it)
                    else if (it.type.contains("管理")) V.mSystemAppList.add(it)
                }
                startMain()
            }
        })
    }

    private fun startMain() {
        startActivity(Intent(context, MainActivity::class.java))
        finish()
    }
}